package org.cli.admin.plugin;

import java.util.List;

import org.cli.admin.util.PlugableUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.AbstractModule;

public class PluginModule extends AbstractModule {
	private static final Logger logger = LoggerFactory
			.getLogger(PluginModule.class);

	@Override
	protected void configure() {
		logger.info("Installing plugins");		
		
		List<Class<? extends IPlugin>> plugins = PlugableUtil.findAndInstall(binder(), Plugin.class, IPlugin.class);
		
		for ( Class<? extends IPlugin> pluginClass : plugins) {
			String name = pluginClass.getAnnotation(Plugin.class).name();
			
			try {
				IPlugin plugin = pluginClass.newInstance();
				plugin.configure(binder());
			} catch (Exception e) {
				logger.error("Plugin implementation should have non-argument constructor");
			}
				
			logger.info("Plugin "+ name +" installed.");
		}
		
		bind(PluginManager.class);
	}

}
